package gridView;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class GraphicTimer extends JPanel {
	
	protected static enum TimeData {
		HOURS,
		MINUTES,
		SECONDES;
	}

	// ATTRIBUTS
	
	private static final long serialVersionUID = 1L;
	
	private static final Font BIG_NUMBER_FONT = new Font("Monospaced", Font.PLAIN, 16);
	private static final Font SMALL_NUMBER_FONT = new Font("Monospaced", Font.PLAIN, 12);
	
	private static final Dimension MINIMUM_SIZE = new Dimension(80, 20);
	
	private static final JLabel SEPARATOR = new JLabel(":");
	
	private Map<TimeData, JLabel> labels;
	
	private long time;
	
	// CONSTRUCTEUR
	
	public GraphicTimer() {
		this(0);
	}
	
	public GraphicTimer(long sec) {
		createView();
		placeComponents();
		setTime(sec);
	}
	
	// REQUETES
	
	public long getTime() {
		return time;
	}
	
	public String getFormattedTime() {
		StringBuffer b = new StringBuffer();
		long h = getHours(),
			m = getMinutes(),
			s = getSeconds();
		if (h > 0) {
			b.append(h + SEPARATOR.getText());
		}
		b.append(addZeros(m, 2) + SEPARATOR.getText() + addZeros(s, 2));
		return b.toString();
	}
	
	public static String getFormattedTime(long sec) {
		StringBuffer b = new StringBuffer();
		long h = getHours(sec),
			m = getMinutes(sec),
			s = getSeconds(sec);
		if (h > 0) {
			b.append(h + SEPARATOR.getText());
		}
		b.append(addZeros(m, 2) + SEPARATOR.getText() + addZeros(s, 2));
		return b.toString();
	}
	
	// COMMANDES
	
	public void setTime(long sec) {
		time = sec;
		refresh();
	}
	
	// OUTILS
	
	private void createView() {
		labels = new HashMap<TimeData, JLabel>();
		for (TimeData d : TimeData.values()) {
			labels.put(d, new JLabel(""));
		}
	}
	
	private void placeComponents() {
		this.setMinimumSize(MINIMUM_SIZE);
		this.setLayout(new FlowLayout(FlowLayout.CENTER));
		labels.get(TimeData.HOURS).setFont(BIG_NUMBER_FONT);
		labels.get(TimeData.MINUTES).setFont(BIG_NUMBER_FONT);
	}
	
	private void refresh() {
		long s = getSeconds(),
				m = getMinutes(),
				h = getHours();
		JLabel ls = labels.get(TimeData.SECONDES),
				lm = labels.get(TimeData.MINUTES),
				lh = labels.get(TimeData.HOURS);
		
		ls.setText(addZeros(s, 2));
		lm.setText(addZeros(m, 2));
		lh.setText("" + h);
		
		removeAll();
		if (h > 0) {
			add(labels.get(TimeData.HOURS));
			add(SEPARATOR);
		}
		add(labels.get(TimeData.MINUTES));
		if (h > 0) {
			labels.get(TimeData.SECONDES).setFont(SMALL_NUMBER_FONT);
		} else {
			labels.get(TimeData.SECONDES).setFont(BIG_NUMBER_FONT);
			add(SEPARATOR);
		}
		add(labels.get(TimeData.SECONDES));
		
		revalidate();
		repaint();
	}
	
	// GADGETS
	
	private long getSeconds() {
		return (time % 60) % 60;
	}
	
	private static long getSeconds(long time) {
		return (time % 60) % 60;
	}
	
	private long getMinutes() {
		return (time / 60) % 60;
	}
	
	private static long getMinutes(long time) {
		return (time / 60) % 60;
	}
	
	private long getHours() {
		return (time / 60) / 60;
	}
	
	private static long getHours(long time) {
		return (time / 60) / 60;
	}
	
	private static String addZeros(long x, int n) {
		StringBuffer s = new StringBuffer("" + x);
		int size = s.length();
		for (int i = size; i < n; i++) {
			s.insert(0, '0');
		}
		return s.toString();
	}
	
}
